#ifndef NOTIFY_MANAGER_H
#define NOTIFY_MANAGER_H
#include"NotifyQueue.h"
#include"NotifyFactory.h"

#include<filesystem>
#include<fstream>
#include<thread>
#include<mutex>
using namespace std;
namespace fs=filesystem;

using NotifyQueue_ptr=unique_ptr<NotifyQueue>;
using Notify_ptr=unique_ptr<Notify>;
class NotificationManager {
    constexpr static char *NOTIFY_DIR="data/notifications/";

    private:
        static NotificationManager *instance;
        NotificationManager();

        unordered_map<int, NotifyQueue_ptr> notify_queues;

    
        string get_notify_file_path(int user_id);

        void create_new_file(string file_path);
        void save_to_file(int user_id, const NotifyQueue_ptr& notify);
        void load_from_file(int user_id, NotifyQueue_ptr& notify); 
        void save_all_notify();

        mutex notify_queues_mutex; // 通知锁
    public:

        NotifyQueue_ptr& QuickLoadNotify(int user_id);

        void add_notify(int user_id,  Notify_ptr notify);

        void send_notify(int user_id, int socket_fd);



        void SaveNotifyToFile_regularly(int clear_frequency);

        void SendOrSaveNotify_regularly(int clear_frequency);

        static NotificationManager* get_instance();

        #ifdef DEBUG
        void save_to_file_debug(int user_id, const NotifyQueue_ptr& notify){
            save_to_file(user_id, notify);
        }
        #endif
};
#endif // NOTIFY_MANAGER_H
